var list = new Array();

$(function() {
	
	for (var i = 0; i < num; i++) {
		list.push(i);
	}
	
});

var savePath = "";
//查看意向结算及分成条款
function showModalBaseitems(t,rowIndex){
	var baseJson = "";
	baseJson += baseToJson(rowIndex).substr(0, baseToJson(rowIndex).length - 1).replace(/\s+/g, '');
	
}

function showModalBrand(id, index, i){
	
}

/**
 * 编辑（查看）意向结算及分成条款
 */
function edit_base(t, rowIndex) {
	savePath = $("#intent_type").val();
	var baseJson = "";
	baseJson += baseToJson(rowIndex).substr(0, baseToJson(rowIndex).length - 1)
			.replace(/\s+/g, '')
	var remarks = $("#remarks_base" + rowIndex).attr("value");
	var isCompare=$("#isCompare_base" + rowIndex).attr("value");
	var contractType =$("#contractType").attr("value");
	$("#assbasemodal").load(Com_Parameter.ContextPath + '/biz/intent/' + savePath+ '/editModalBase?contractType='+contractType+'&baseJson=' + baseJson + '&num='+ rowIndex,function() {
		$("#assbasemodal").modal("show");
		$("#remarks_base").val(remarks)
		$("#startDate_base").val($("#startDate_base" + rowIndex).attr("value"));
		$("#endDate_base").val($("#endDate_base" + rowIndex).attr("value"));
		$("#isCompare").val(isCompare);
	});
}

/**
 * 查看（编辑）意向合同条款信息
 */
function edit_items(t, rowIndex) {
	savePath = $("#intent_type").val();
	var itemsJson = "";
	var deductItem = $("#deductItem_items" + rowIndex).attr("value");
	var taxRate = $("#taxRate_items" + rowIndex).attr("value");
	var remarks = $("#remarks_items" + rowIndex).val();
	var contracttype = $("#contract_type").val();
	var businessType = $("#businessType").val();
	itemsJson += itemsToJson(rowIndex).substr(0,itemsToJson(rowIndex).length - 1).replace(/\s+/g, '');
	var path =Com_Parameter.ContextPath + '/biz/intent/' + savePath+ '/editAssitems?itemsJson='+itemsJson+'&num='+rowIndex+'&contractType='+contracttype+'&supplierOption='+businessType
	$("#intAssitems").load(path,function() {
		
		 $('#deductCode', this).chosen('destroy').chosen();
		$("#intAssitems").modal("show");
		$("#taxRate_items").val(taxRate);
		$("#deductItem").val(deductItem);
		$("#remarks_items").val(remarks)
		$("#startDate_items").val(
				$("#startDate_items" + rowIndex).attr("value"));
		$("#endDate_items").val(
				$("#endDate_items" + rowIndex).attr("value"));

	});
}



/**
 * 编辑（查看）品牌信息
 */
function edit_brand(t, rowIndex) {
	var barndJson = "";
	var shopId = $("#intAssShopIds" + rowIndex).val();
	var shopName = $("#intAssShopNames" + rowIndex).val();
	var brandEName = $("#brandEName" + rowIndex).attr("value");
	var brandCName = $("#brandCName" + rowIndex).attr("value");
	var supplierNo =$("#supplierNo").attr("value");
	var brandId =$("#brandId"+rowIndex).attr("value");
	barndJson += brandToJson(rowIndex).substr(0,brandToJson(rowIndex).length - 1).replace(/\s+/g, '');
	var path =Com_Parameter.ContextPath+'/biz/intent/common/editIntAssbrand?assBrand='+barndJson+'&num='+rowIndex+"&supplierNo="+supplierNo+"&brandId="+brandId;
	$("#barndModal").load(path, function() {
		$("#barndModal").modal("show");
		$("#intAssShopIds").val(shopId);
		$("#intAssShopNames").val(shopName);
		$("#brandEName").val(brandEName);
		$("#brandCName").val(brandCName);

	});
}


/*******************************
 * 品牌封装json
 *******************************/
var brandToJson = function(index) {
	var jsonStr = "";
	var id= $("#intAssbrand_id"+index) .val();
	var brandId= $("#brandId"+index).val();
	var brandCName= $("#brandCName"+index).val();
	var brandEName= $("#brandEName"+index).val();
	var brandLevel= $("#brandLevel"+index).val();
	var memberRateType= $("#memberRateType"+index).val();
	var commodityCate= $("#commodityCate"+index).val();
	var gcardDiscount= $("#gcardDiscount"+index).val();
	var gcardDiscount2= $("#gcardDiscount2"+index).val();
	var ocardDiscount= $("#ocardDiscount"+index).val();
	var ocardDiscount2= $("#ocardDiscount2"+index).val();
	var inviteCate= $("#inviteCate"+index).val();
	var isMainBrand= $("#isMainBrand"+index).val();
	var brandInviteHead= $("#brandInviteHead"+index).val();
	var investmentCode= $("#investmentCode"+index).val();
	var brandNo= $("#brandNo"+index).val(); 
	var brandAttribute= $("#brandAttribute"+index).val();
	jsonStr += "{'id':'" + id + "','brandId':'" + brandId
			+ "','brandCName':'" + brandCName + "','brandEName':'"
			+ brandEName + "',"
	jsonStr += "'brandLevel':'" + brandLevel + "','memberRateType':'" + memberRateType
			+ "','commodityCate':'" + commodityCate + "',"
	jsonStr += "'gcardDiscount':'" + gcardDiscount + "','gcardDiscount2':'"
			+ gcardDiscount2 + "','ocardDiscount2':'" + ocardDiscount2
			+ "','ocardDiscount':'" + ocardDiscount + "',"
	jsonStr += "'inviteCate':'" + inviteCate + "','isMainBrand':'"
			+ isMainBrand + "','brandInviteHead':'" + brandInviteHead
			+ "','investmentCode':'" + investmentCode + "','brandNo':'"
			+ brandNo + "','brandAttribute':'"+brandAttribute+"'";
	jsonStr += "},";
	return jsonStr;
}

/*****************************************
 * 结算及分成条款封装成json
 ***********************************/
var baseToJson = function(index){
	var jsonStr ="";
	var baseitems_id   =$("#intAssbaseitems_id"+index).val();
	var	brandsId       =$("#barndId_base"+index).val();
	var	isGuarantee    =$("#isGuarantee_base"+index).val();
	var	codeType       =$("#codeType_base"+index).val();
	var	countBase      =$("#countBase_base"+index).val();
	var	deductType     =$("#deductType_base"+index).val();
	var	startDate      =$("#startDate_base"+index).val();isCompare
	var	isCompare      =$("#isCompare_base"+index).val();
	var	endDate        =$("#endDate_base"+index).val();
	var	validType      =$("#validType_base"+index).val();
	var	guaranteeType  =$("#guaranteeType_base"+index).val();
	var	guaranteeAmount=$("#guaranteeAmount_base"+index).val();
	var	lower          =$("#lower_base"+index).val();
	var	upper          =$("#upper_base"+index).val();
	var	remarks        =$("#remarks_base"+index).val();
	var	deductRate     =$("#deductRate_base"+index).val();
	var payDate        =$("#payDate_base"+index).val();
	var isCommGuarantee  =$("#isCommGuarantee_base"+index).val();
	jsonStr +="{'id':'"+baseitems_id+"','brandsId':'"+brandsId+"','isGuarantee':'"+isGuarantee+"','codeType':'"+codeType+"',";
	jsonStr +="'countBase':'"+countBase+"','deductType':'"+deductType+"','startDate':'"+startDate+"','endDate':'"+endDate+"','validType':'"+validType+"',"
	jsonStr +="'guaranteeType':'"+guaranteeType+"','guaranteeAmount':'"+guaranteeAmount+"','isCommGuarantee':'"+isCommGuarantee+"',";
	jsonStr +="'lower':'"+lower+"','upper':'"+upper+"','remarks':'"+remarks+"','deductRate':'"+deductRate+"','payDate':'"+payDate+"','isCompare':'"+isCompare+"'},";
	return jsonStr;
}

/**封装合同条款json
 * 
 * @param t
 * @param index
 */
var itemsToJson = function(index){
	var jsonStr = "";
	var	id     = $("#intAssitems_id"+index).val();
	var	deductItem   = $("#deductItem_items"+index).val();
	var	deductCate   = $("#deductCate_items"+index).val();
	var	amount       = $("#amount_items"+index).val();
	var	startDate    = $("#startDate_items"+index).val();
	var	endDate      = $("#endDate_items"+index).val();
	var	deductType   = $("#deductType_items"+index).val();
	var	deductCode   = $("#deductCode_items"+index).val();
	var	taxRate      = $("#taxRate_items"+index).val();
	var	countBase    = $("#countBase_items"+index).val();
	var	remarks      = $("#remarks_items"+index).val();
	var	validType    = $("#validType_items"+index).val();
	jsonStr += "{'id':'"+id+"','deductItem':'"+deductItem+"','deductCate':'"+deductCate+"','amount':'"+amount+"','startDate':'"+startDate+"'," ;
	jsonStr += "'endDate':'"+endDate+"','deductType':'"+deductType+"','deductCode':'"+deductCode+"','taxRate':'"+taxRate+"','countBase':'"+countBase+"'," ;
	jsonStr += "'remarks':'"+remarks+"','validType':'"+validType+"'},";
	return jsonStr;
}

//供应商确认合作
function supplierAgree(id){
	var id = $("#id").attr("value");
	var intentType = $("#intent_type").attr("value");
	var supplierConfirm =$("#supplierConfirm").attr("value");
	top.$.jBox.confirm('您确定同意合作吗？','系统提示',function(v,h,f){
		if(v=="ok"){
			//修改意向状态为供应商已经同意
			Oosp_Loading('正在提交，请稍等...');
			if(supplierConfirm =="1"){
				top.$.jBox.tip('您已经确认合作！无须再次确认！', 'warning');
				return;
			}
			$.get(Com_Parameter.ContextPath+'/biz/intent/common/'+id+'/updateSupplierConfirm?supplierConfirm=1',function(data){
				var obj = eval('(' + data + ')');
				if(obj.success){
					$("#unagree").hide();
					$("#agree").hide();
					top.$.jBox.tip('确认合作成功！', 'success');
				}
			});
		}
		return;
	});
}

//供应商不同意合作
function supplierUnagree(){
	var id = $("#id").attr("value");
	top.$.jBox.confirm('您确定不 同意合作吗？如果不同意将重新开始洽谈。','系统提示',function(v,h,f){
		if(v=="ok"){
			$.get(Com_Parameter.ContextPath+'/biz/intent/common/'+id+'/updateSupplierConfirm?navId='+navId,function(data){
				var obj = eval('(' + data + ')');
				if(obj.success){
					$("#unagree").hide();
					$("#agree").hide();
					top.$.jBox.tip('此次合作取消，已通知采购重新开始洽谈！', 'success');
				}
			});
		}
		return;
	});
}

/**
 * 
 * @param t
 * @param opt
 * @param id
 */
function changChecked(t,opt,id){
	 $("[id = attList"+t+"isContrfim]:checkbox").each(function () {
		var confim=0;
        if($(this).attr("checked")=='checked'){
        	$(this).attr("value",'1');
        	confim=1;
        
        }else{
        	$(this).attr("value",'0');
        }
        
    	if(opt=='view'){
    		if(id!=''){
    			$.getJSON(Com_Parameter.ContextPath + "/biz/intent/common/editAtt?id="+id+"&confim="+confim, function(data) {
				});
    		}
    	}
     });

}

function remove_Att(t,index,id){
	var tr = $(t).closest("tr");
	top.$.jBox.confirm('确认要删除该条数据吗？','系统提示',function(v,h,f){
		if(v=='ok'){	
			tr.remove();
			if(id!=''){
				$.getJSON(Com_Parameter.ContextPath + "/biz/intent/common/removeAtt?id="+id, function(data) {
				});
			}
			
		}
		
	},{buttonsFocus:1});
}
/**
 * 终止意向提报
 */
function closeIntent(){
	var id = $("#id").attr("value");
	top.$.jBox.confirm('您确认要终止该意向吗？','系统提示',function(v,h,f){
		if(v=="ok"){
			Oosp_Loading('正在终止意向，请稍等...');
			$.get(Com_Parameter.ContextPath+'/biz/intent/common/'+id+'/closeIntent?navId='+navId,function(data){
				var obj = eval('(' + data + ')');
				if(obj.success){
					$("#hidden").hide();
					top.$.jBox.tip('该意向已经终止，可以重新开始洽谈！', 'success');
					window.location.reload();//刷新当前页面

				}
			});
		}
		return;
	});
}
/**
 * 查看已生成的合同
 */
function contractView(){
	var intentType = $("#intent_type").attr("value");
	var id = $("#id").attr("value");
	var navId = $("#navId").attr("value");
	window.open(Com_Parameter.ContextPath+'/biz/intent/'+intentType+'/getContarctByIntId?id='+id+'&navId='+navId,'_blank');
}


/**
 * 通知供应商下载合同附件
 */

function sendMessageToSupplier(id,navId,intentType){
	
	top.$.jBox.confirm('是否确定通知供应商下载协议文本','系统提示',function(v,h,f){
		if(v=="ok"){
			Oosp_Loading('正在发送通知，请稍等...');
			$.get(Com_Parameter.ContextPath+'/biz/intent/common/'+id+'/sendMessage?navId='+navId+"&intentType"+intentType,function(data){
				var obj = eval('(' + data + ')');
				if(obj.success){
					top.$.jBox.tip('已通知供应商下载合同！', 'success');
				}
			});
		}
		return;
	});
}


